package com.hengtiansoft.lfy.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.hengtiansoft.lfy.R;
import com.hengtiansoft.lfy.base.BaseActivity;
import com.hengtiansoft.lfy.entity.ServiceResult;
import com.hengtiansoft.lfy.entity.TokenInfo;
import com.hengtiansoft.lfy.net.NetHelper;
import com.hengtiansoft.lfy.net.NetUtils;
import com.hengtiansoft.lfy.utils.Config;
import com.hengtiansoft.lfy.utils.Utils;

/**
 * Created by daiye on 2016/10/27 0027.
 */

public class RegisterActivity extends BaseActivity {
    private static final String TAG = RegisterActivity.class.getSimpleName();
    private final int ACTIVITY_RESULT_REGISTER_MOBILE = 1;

    private ImageView iv_back = null;
    private EditText mUserNameEt;
    private EditText mPasswordEt;
    private EditText mPasswordAgainEt;
    private Button mRegisterBt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initView();
    }

    private void initData(){

    }

    private void initView() {
        setContentView(R.layout.activity_register);
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            public final void onClick(View v) {
                finish();
            }
        });

        mUserNameEt = (EditText) findViewById(R.id.username_et);
        mPasswordEt = (EditText) findViewById(R.id.password_et);
        mPasswordAgainEt = (EditText) findViewById(R.id.password_again_et);

        mRegisterBt = (Button)findViewById(R.id.register_bt);
        mRegisterBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InputMethodManager inputManager = (InputMethodManager) mRegisterBt.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(mRegisterBt, 0);
                processRegister();
            }
        });

        mPasswordEt.setTypeface(Typeface.DEFAULT);
        mPasswordEt.setTransformationMethod(new PasswordTransformationMethod());
        mPasswordAgainEt.setTypeface(Typeface.DEFAULT);
        mPasswordAgainEt.setTransformationMethod(new PasswordTransformationMethod());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public String userNameStr;
    public String firstPasswordStr;

    private void processRegister() {
        // 用户注册信息提交
        userNameStr= mUserNameEt.getText().toString();
        firstPasswordStr = mPasswordEt.getText().toString();
        final String secondPasswordStr = mPasswordAgainEt.getText().toString();

        if(TextUtils.isEmpty(userNameStr) ){
            Toast.makeText(this, R.string.login_username_empty_tips, Toast.LENGTH_SHORT).show();
            return;
        }

        if(TextUtils.isEmpty(firstPasswordStr) ){
            Toast.makeText(this, R.string.login_password_empty_tips, Toast.LENGTH_SHORT).show();
            return;
        }
        if(!firstPasswordStr.equals(secondPasswordStr)){
            Toast.makeText(this, R.string.register_pwd_different_tips, Toast.LENGTH_SHORT).show();
            return;
        }
//        if( firstPasswordStr.length() < 6 ){
//            Toast.makeText(this, R.string.passwd_less6, Toast.LENGTH_SHORT).show();
//            return;
//        }

        registerServer();
    }

    private void registerServer() {
        NetHelper.userRegister( RegisterActivity.this, userNameStr, Utils.MD5(firstPasswordStr.trim()), null,  new NetUtils.NetCallBack<ServiceResult>() {
            @Override
            public void success(ServiceResult rspData) {
                if (rspData.getRetcode() == 0) {
                    getUserToken();
                    Toast.makeText(RegisterActivity.this, rspData.getMessage(), Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(RegisterActivity.this, rspData.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void failed(String msg) {
                Toast.makeText(RegisterActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        }, ServiceResult.class);
    }

    private void getUserToken(){
        NetHelper.getUserToken( RegisterActivity.this, new NetUtils.NetCallBack<ServiceResult>() {
            @Override
            public void success(ServiceResult rspData) {
                TokenInfo info = (TokenInfo)rspData;
                if (info.getRetcode() == 0) {
                    String token =  info.getData();
                    Config.setUserToken(token);

                    Intent toIntent = new Intent(RegisterActivity.this, RegisterMobileActivity.class);
                    toIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    Bundle bundle = new Bundle();
                    bundle.putString("UserName", userNameStr);
                    bundle.putString("Password", firstPasswordStr);
                    toIntent.putExtras(bundle);
                    startActivityForResult(toIntent, ACTIVITY_RESULT_REGISTER_MOBILE);
                } else {
                    Toast.makeText(RegisterActivity.this, rspData.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void failed(String msg) {
                Toast.makeText(RegisterActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        }, TokenInfo.class);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ACTIVITY_RESULT_REGISTER_MOBILE) {
            Intent intent = new Intent();
            intent.putExtra("UserName", userNameStr);
            intent.putExtra("Password", firstPasswordStr);
            setResult(RESULT_OK, intent);
            RegisterActivity.this.finish();
        }
    }
}
